package DataBase;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

public class GetItemsForOrder {

	private Vector<Vector> orders;
	private int order_id;

	public GetItemsForOrder(int order_id) {
		this.order_id = order_id;
		this.orders = new Vector<Vector>();
	}

	public Vector<Vector> getOrders() {
		SQLConnection sc = new SQLConnection();
		String sql = "SELECT item.Iname ,"
				+ "items_in_order.amount, "
				+ "items_in_order.price, "
				+ " orders.date," 
				+ " orders.time,"
				+ "supplier.suppName,"
				+ "supplier.suppPhoneNo,"
				+ "supplier.suppCity,"
				+ "supplier.suppStreet,"
				+ "supplier.suppHouse, "
				+ " orders.order_id "
				+ "FROM orders "
				+ "JOIN items_in_order ON orders.order_id = items_in_order.order_id "
				+ "JOIN item ON item.Iid = items_in_order.item_id "
				+ "JOIN supplier ON orders.supplier_id = supplier.suppID  "
				+ "WHERE items_in_order.order_id = ? AND item.Iid = items_in_order.item_id;";

		try {
			PreparedStatement ps = sc.getConnection().prepareStatement(sql);
			ps.setInt(1,order_id);
			ResultSet rs = ps.executeQuery();

			ResultSetMetaData rsmd = rs.getMetaData();

			while (rs.next()) {
				Vector<String> row = new Vector<String>();
				for (int col = 1; col <= rsmd.getColumnCount(); col++) {
					row.add(rs.getString(col));
				}
				orders.add(row);

			}
			ps.close();
			rs.close();
			sc.getConnection().close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return orders;
	}
}
